<template>
  <el-row :gutter="10">
    <el-col :span="18">
      <el-table :data="list">
        <el-table-column label="备品备件" min-width="50%" prop="part_id" />
        <el-table-column label="计划数量" min-width="52%" prop="number" />
        <el-table-column label="操作" min-width="8%">
          <template v-slot="scope">
            <i class="el-icon-delete" @click="remove(scope.$index, scope.row.id)" />
          </template>
        </el-table-column>
        <div slot="append">
          <el-form :model="data" inline>
            <el-input v-model="data.id" type="hidden" />
            <el-row :gutter="10">
              <el-col :span="11">
                <el-form-item label>
                  <v-select v-model="data.part_id" action="part" />
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label>
                  <el-input v-model="data.number" placeholder />
                </el-form-item>
              </el-col>
              <el-col :span="2">
                <el-form-item label>
                  <el-button icon="el-icon-upload2" @click="save" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </el-table>
    </el-col>
    <el-col :span="6">
      <el-link type="primary">物料</el-link>
    </el-col>
  </el-row>
</template>

<script>
import VSelect from '@/components/select'
import init from '@/mixins/init'
import API from '@/api/project/plan'
export default {
  components: { VSelect },
  mixins: [init],
  data() {
    return {
      data: {},
      list: []
    }
  },
  watch: {
    list() {
      this.$emit('total', { component: 'part', total: this.list.length })
    }
  },
  methods: {
    index() {
      API.parts({ plan_id: this.value }).then(response => {
        this.list = response.data.data
      })
    },
    save() {
      API.part({ plan_id: this.value, ...this.data }).then(response => {
        if (!this.data.id) {
          this.data.id = response.data.id
          this.list.push(this.data)
        }
      })
    }
  }
}
</script>
